package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecLessonLessonCrtController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        YxyProfile yxyProfile = getProfile(request, response);
        String author = yxyProfile.getUserNick();
        String authorId = yxyProfile.getId();
        String avator = yxyProfile.getUserAvator();
        String schoolId = yxyProfile.getSchoolId();
        String schoolNick = yxyProfile.getSchoolName();
        String name = StringTrimUtil.trim(reqModel.name);
        String summary = StringTrimUtil.trim(reqModel.summary);
        String cover = reqModel.cover;
        String catalog1 = reqModel.catalog1;
        String catalog2 = reqModel.catalog2;
        String catalog3 = reqModel.catalog3;
        String catalog4 = reqModel.catalog4;
        String catalog5 = reqModel.catalog5;

        // 没有上传封面图片
        if (StringUtils.isBlank(cover)) {
            // 0-2的随机数
            java.util.Random random = new java.util.Random();
            int i = random.nextInt(3);
            if (i == 1) {
                cover = "https://yxyosc.91yunxiao.com/yxyosc/2018/01/11/16d9e830201cfd2658b8128e7c41fa49.jpg";
            } else if (i == 2) {
                cover = "https://yxyosc.91yunxiao.com/yxyosc/2018/01/11/e5f6509d3a9ee3193d259dd80dbc4adc.jpg";
            } else {
                cover = "https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/4c3e5c520a019c3701287e96c9cfd682.jpg";
            }
        }
        YxyLessonsCloudLesson lesson = new YxyLessonsCloudLesson();
        lesson.setName(name);
        lesson.setSummary(summary);
        lesson.setCover(cover);
        lesson.setCatalogId1(catalog1);
        lesson.setCatalogId2(catalog2);
        lesson.setCatalogId3(catalog3);
        lesson.setCatalogId4(catalog4);
        lesson.setCatalogId5(catalog5);
        lesson.setCount(0);
        lesson.setCreateDate(new Date());
        lesson.setUpdateDate(new Date());
        lesson.setStatus(0);
        lesson.setPv(0);
        lesson.setVv(0);
        lesson.setIsVisible(0);
        lesson.setSchoolId(schoolId);
        lesson.setSchoolNick(schoolNick);
        lesson.setAuthor(author);
        lesson.setAuthorId(authorId);
        lesson.setAvator(avator);
        lesson = lessonRepository.saveAndFlush(lesson);

        RespModel respModel = new RespModel();
        respModel.lessonId = lesson.getId();
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String name;
        public String summary;
        public String cover;
        public String catalog1;
        public String catalog2;
        public String catalog3;
        public String catalog4;
        public String catalog5;

        @Override
        public void autoCheck() {

        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String lessonId;
    }
}
